package com.apestech.api.workflow.lib.app;

import com.alibaba.druid.pool.DruidDataSource;
import com.apestech.api.workflow.extend.FlowDatabase;
import com.apestech.api.workflow.lib.FlowUtil;
import com.apestech.api.workflow.lib.element.FlowExecute;
import com.apestech.api.workflow.lib.element.FlowTask;
import com.apestech.api.workflow.lib.element.WorkProcessEngine;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.DBUtil;

/**
 * 改签应用
 *
 * @author hup
 * @create 2018-07-21-14:36
 */
public class ChangeSignApp extends FlowUtil{

    private WorkProcessEngine engine;
    private SqlSession session;
    private FlowTask flowTask;
    private JSONObject input;
    private String processInstanceId;
    private String state;
    private String taskId;
    private String taskName;

    public ChangeSignApp(WorkProcessEngine engine, JSONObject input) throws Exception {
        this.engine=engine;
        this.session= DBUtil.getSession(this.engine.getDataSource());
        this.session.setConnection(this.engine.getConnection());
        this.input=input;
        this.processInstanceId=input.getString("processInstanceId");
        this.taskId=input.getString("taskId");
        if(this.processInstanceId==null && this.taskId==null){
            throw new Exception("流程ID或任务ID不允许为空！");
        }
        flowTask= getFlowTask(engine,  this.input);
        if(this.flowTask.getTask().isSuspended()){
            throw new Exception("该任务为挂起状态，不允许做转签相关操作！");
        }
        this.input.put("processInstanceId", flowTask.getProcessInstanceId());
        this.state=flowTask.getState();
        this.taskId=flowTask.getTask().getId();
        this.taskName=flowTask.getTask().getName();
    }

    /**
     * 改签
     * @throws Exception
     */
    public void changeSignTask() throws Exception {
        preInputValidate(input, true);
        if(!("claim".equals(state) || "addSign".equals(state) || "complete".equals(state) || "assgin".equals(state) )){
            throw new Exception("当前状态不允许做转签操作！");
        }
        if(flowTask.getTask().getAssignee()!=null){
            if(!input.getString("loginUserId").equals(flowTask.getTask().getAssignee())){
                throw new Exception("当前结点发起人["+input.getString("loginUserId")+"]不是结点经办人["+flowTask.getTask().getAssignee()+"]，请重新发起！");
            }
        }
        String userId=input.getString("userId");
        String userName=input.getString("userName");
        FlowDatabase.UserInfo user=getCommission(engine, userId, userName);
        userId=user.getId();
        userName=user.getName();
        input.put("userId", userId);
        input.put("userName", userName);
        FlowExecute exec=new FlowExecute(engine);
        if(flowTask.getTask().getAssignee()!=null){
            exec.unClaimTask(taskId);
        }
        exec.deleteTaskAllUser(taskId);
        exec.claimTask(taskId, userId);
        writeTaskDb(engine, session, input, taskId, taskName, ExtendDbType.CHANGE_SIGN);
    }

}
